package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.FormalStudent;
import cn.wolfcode.crm.domain.RemoveHistory;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.service.IFormalStudentService;
import cn.wolfcode.crm.service.IRemoveHistoryService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private IRemoveHistoryService removeHistoryService;
    @Autowired
    private IFormalStudentService formalStudentService;

    public int insert(Client record) {
        record.setInputTime(new Date());
        record.setSalemen(UserContext.getCurrentUser());
        return clientMapper.insert(record);
    }

    public int updateByPrimaryKey(Client record) {
        return clientMapper.updateByPrimaryKey(record);
    }

    public List<Client> selectAll() {
        return clientMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int total = clientMapper.query4Count(qo);
        if (total > 0) {
            return new PageResult(total, clientMapper.query4List(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    public int deleteByPrimaryKey(Long id) {
        return clientMapper.deleteByPrimaryKey(id);
    }

    public PageResult query4Prospect(QueryObject qo) {
        int total = clientMapper.query4ProspectCount(qo);
        if (total > 0) {
            return new PageResult(total, clientMapper.query4Prospect(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    public void follow(Long id) {
        Client client = clientMapper.selectByPrimaryKey(id);
        RemoveHistory removeHistory = new RemoveHistory();
        removeHistory.setMoveUser(client.getSalemen());
        removeHistory.setRowUser(UserContext.getCurrentUser());
        removeHistory.setHirdate(new Date());
        removeHistory.setName(client.getName());
        removeHistory.setQq(client.getQq());
        removeHistory.setTel(client.getTel());
        removeHistoryService.insert(removeHistory);
        clientMapper.updateClient(id, UserContext.getCurrentUser().getId(), new Date());
    }

    public void resourcePool(Long clientId) {
        clientMapper.resourcePool(clientId);
    }

    public void positive(Long clientId, FormalStudent formalStudent) {
        clientMapper.positive(clientId);
        formalStudent.setStatus(0);
        formalStudent.setClientId(clientId);
        //计划学费
        BigDecimal programTuition = formalStudent.getProgramTuition();
        //优惠金额
        BigDecimal discountAmount = formalStudent.getDiscountAmount();
        //其他优惠
        BigDecimal otherDiscount = formalStudent.getOtherDiscount();
        //其他费用
        BigDecimal otherExpense = formalStudent.getOtherExpense();
        //预付学费
        BigDecimal prepaidTuition = formalStudent.getPrepaidTuition();
        //已付学费
        BigDecimal planTuition = formalStudent.getPlanTuition();
        //计算总费用
        BigDecimal add = programTuition.add(otherExpense);
        //计算还欠学费
        BigDecimal debt = add.subtract(discountAmount.add(otherDiscount).add(prepaidTuition).add(planTuition));
        if (debt.compareTo(BigDecimal.ZERO) > 0) {
            formalStudent.setCompletePayment(false);
            formalStudent.setDebt(debt);
        } else {
            formalStudent.setDebt(BigDecimal.ZERO);
            formalStudent.setCompletePayment(true);
        }
        formalStudent.setTotalTuition(add);
        formalStudentService.insert(formalStudent);
    }

    public void updateByStudentTracking(Client client) {
        clientMapper.updateByStudentTracking(client);
    }

    public void updateByTracking(Long id) {
        clientMapper.updateByTracking(id);
    }

    public void updateClient(Client client) {
        clientMapper.updateClientInfo(client);
    }

    public Boolean validateName(String tel, String name) {
         Client client = clientMapper.getClient(tel,name);
         if(client != null){
             return true;
         }else{
             return false;
         }
    }
}
